<?php

define("PRIORITY_NORMAL", 3);

class Application_Form_Task extends Zend_Form {

   public function init() {
      $this->setName('task');

      $id = new Zend_Form_Element_Hidden('id');
      $id->addFilter('Int');

      $description = new Zend_Form_Element_Textarea('description');
      $description->setLabel('Description')
			->setRequired(true)
			//->addFilter('StripTags')
			->addFilter('StringTrim')
         ->setAttrib('rows', 10)
         ->setAttrib('class', 'ckeditor')
         ->setDecorators(array(
            'ViewHelper',
            'Description',
            'Errors',
            array('HtmlTag', ''),
            array('Label', ''),
         ));

      $status = new Application_Model_DbTable_Status();
      $resultSet = $status->fetchAll(null, 'cardinal ASC');

      $status_id = new Zend_Form_Element_Select('status_id');
      $status_id->setLabel('Status')
           ->setRequired()
           ->setDecorators(array(
               'ViewHelper',
               'Description',
               'Errors',
               array('HtmlTag', ''),
               array('Label', ''),
      ));

      foreach ($resultSet as $row) {
         $status_id->addMultiOption($row->id, $row->description);
      }

      $submit = new Zend_Form_Element_Submit('submit');
		$submit->setAttrib('id', 'submitbutton')
			->setLabel('Save')
         ->setAttrib('class', 'btn btn-primary');

      $this->addElements(array(
          $id,
          $description,
          $status_id,
          $this->_priorities(),
          $this->_deadline(),
          $submit
      ));

      $this->addDisplayGroup(array('submit'), 'form_actions', array(
          'decorators' => array(
            'FormElements',
            'Fieldset',
            array('HtmlTag', array('tag' => 'div', 'class' => 'form-actions'))
          )
      ));
   }
   
   protected function _priorities() {
      $e = new Zend_Form_Element_Select('priority_id');
      $e->setRequired(true)
              ->setLabel('Priority:')
              ->setDecorators(array(
               'ViewHelper',
               'Description',
               'Errors',
               array('HtmlTag', ''),
               array('Label', ''),
      ));
      
      $model = new Application_Model_DbTable_Priorities();
      $rs = $model->fetchAll(null, 'cardinal ASC');
      foreach ($rs as $row) {
         $e->addMultiOption($row->id, $row->description);
      }
      // default value
      $e->setValue(PRIORITY_NORMAL);
      
      return $e;
   }
   
   protected function _deadline() {
      $e = new Zend_Form_Element_Text('deadline');
      $e->setLabel('Deadline:')
              ->setRequired(true)
              ->addFilter('StripTags')
              ->addFilter('StringTrim')
              ->setAttrib('class', 'span2 date');

      $e->setDecorators(array(
          'ViewHelper',
          'Description',
          'Errors',
          array('HtmlTag', ''),
          array('Label', ''),
      ));
      return $e;
   }
}

